<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>登录</title>
    <script type="text/javascript" src="./script/base.js"></script>
    <link rel="stylesheet" type="text/css" href="./css/api.css" />
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.min.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/api.js"></script>
    <script type="text/javascript" src="./script/aui-tab.js"></script>
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <script type="text/javascript" src="./script/aui-slide.js"></script>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
    <script type="text/javascript">
        function closeWin() {
            api.closeFrame({
                name: 'login'
            });
        };
    </script>
</head>

<body>
    <!-- 头部 -->
    <header class="aui-bar aui-bar-nav" id="topbar">
        <a class="aui-pull-left aui-btn">
            <span class="aui-iconfont aui-icon-left" tapmode onclick="closeWin();"></span>
        </a>
        <div class="aui-title">登录</div>
    </header>

    <div class="register">
        <div class="aui-content aui-margin-b-15">
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">
                            账号
                        </div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="请输入您的手机号码/用户名" id="mobile">
                            <div class="check" id="checkPhone"></div>
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">
                            登录密码
                        </div>
                        <div class="aui-list-item-input">
                            <input type="password" placeholder="请输入密码" id="PWD">
                            <div class="check" id="checkPSW"></div>
                        </div>
                    </div>
                </li>
            </ul>
            <div class="register-d">
                <span class="aui-pull-left" tapmode onclick="forgetPwd();">忘记密码</span>
                <span class="aui-pull-right" tapmode onclick="register();">注册账号</span>
            </div>
            <div class="register-btn" tapmode onclick="login();">登录</div>
        </div>
    </div>

</body>
<script type="text/javascript">
    apiready = function() {

    }

    // 登录
    function login() {
        var mobile = $api.byId('mobile');
        var mobileValue = mobile.value;
        lastMobileValue = mobileValue;

        // 手机号不能为空
        if (!mobileValue) {
            $api.byId('checkPhone').innerText = '手机号不能为空';
            return false;
        } else {
            $api.byId('checkPhone').innerText = '';
        }

        var PWD = $api.byId('PWD');
        var PWDValue = PWD.value;
        // 密码不能为空
        if (!PWDValue) {
            $api.byId('checkPSW').innerText = '密码不能为空';
            return false;
        } else {
            $api.byId('checkPSW').innerText = '';
        }

        var funName = 'updateData();';
        api.ajax({
            url: webAddress + '/index.php?ctl=app_user_center&act=mobiledologin&ajax=1',
            method: 'post',
            data: {
                values: {
                    mobile: mobileValue,
                    user_pwd: PWDValue,
                },
            }
        }, function(ret, err) {
            if (ret) {
                var rett = ret;
                if (ret.status == 0) {
                    alert(ret.info);
                    PWD.focus();
                    return false;
                } else {
                    //  判断系统版本  ios 或者安卓
                    if (api.systemType == 'ios') {
                        // $api.setStorage('password', null);
                        var password = $api.getStorage('password');
                        if (!password) {
                            api.openWin({
                                name: 'ios-lock',
                                url: './ios-lock.html',
                                bgColor:'#00744b',
                                slidBackEnabled:false,
                                pageParam: {
                                    rett: rett,
                                    mobileValue: mobileValue,
                                }
                            });
                        }else {
                        // 已经添加手势
                        showToast();
                        var userid = rett.user.id;

                        $api.setStorage('userid', userid);
                        $api.setStorage('mobile', mobileValue);

                        var param = api.pageParam.login;
                        if (param == 'registerRedPacket' || param == 'tiyanjin') {

                            sendMsg();
                            // 上页面刷新数据
                            var refreshUser = 'gotoUser();';
                            api.execScript({
                                name: 'home',
                                script: refreshUser,
                            });

                            // 成功后关闭登录页面
                            api.closeToWin({
                                name: 'home'
                            });

                        } else {
                            var param = api.pageParam.login;
                            if (param == 'licai') {
                            // 上页面刷新数据
                            var refreshUser = 'gotoUser();';
                            api.execScript({
                                name: 'home',
                                script: refreshUser,
                            });
                            api.closeFrame({
                                name: 'login'
                            });

                        }else{
                         sendMsg();
                            // 登录成功后 跳转到user页面调用刷新数据方法
                            api.setFrameGroupIndex({
                                name: 'nav',
                                index: 2,
                                reload: true,
                            });
                            api.closeFrame({
                                name: 'login'
                            });
                        }
                    }
                }

            } else {
                androidLock(rett,mobileValue);

            }
        }
    } else {
        alert(err.msg);
    }
});
    }


    function androidLock(rett,mobileValue) {
        var isLock = $api.getStorage('isLock');
        if (!isLock) {
            alert('请添加手势!');
            var screenLock = api.require('screenLock');
            screenLock.set({
                color: '#00744b'
            }, function(ret, err) {
                // 添加成功
                if (ret) {
                    $api.setStorage('isLock', true);
                    showToast();
                    var userid = rett.user.id;
                    $api.setStorage('userid', userid);
                    $api.setStorage('mobile', mobileValue);


                    var param = api.pageParam.login;
                    if (param == 'registerRedPacket' || param == 'tiyanjin') {

                        sendMsg();
                        // 上页面刷新数据
                        var refreshUser = 'gotoUser();';
                        api.execScript({
                            name: 'home',
                            script: refreshUser,
                        });

                        // 成功后关闭登录页面
                        api.closeToWin({
                            name: 'home'
                        });

                    } else {
                        var param = api.pageParam.login;
                        if (param == 'licai') {
                        // 上页面刷新数据
                        var refreshUser = 'gotoUser();';
                        api.execScript({
                            name: 'home',
                            script: refreshUser,
                        });
                    }
                    sendMsg();
                          // 登录成功后 跳转到user页面调用刷新数据方法
                          api.setFrameGroupIndex({
                            name: 'nav',
                            index: 2,
                            reload: true,
                        });

                          api.closeFrame({
                            name: 'login'
                        });
                      }

                    // // 登录成功后 跳转到user页面调用刷新数据方法
                    // api.setFrameGroupIndex({
                    //     name: 'nav',
                    //     index: 2,
                    //     reload: true,
                    // });

                    // // 成功后关闭登录页面
                    // api.closeFrame({
                    //     name: 'login'
                    // });

                } else {
                    alert('添加失败');
                }
            });

        } else {

            // 已经添加手势
            showToast();
            var userid = rett.user.id;

            $api.setStorage('userid', userid);
            $api.setStorage('mobile', mobileValue);

            var param = api.pageParam.login;
            if (param == 'registerRedPacket' || param == 'tiyanjin') {

                sendMsg();
                // 上页面刷新数据
                var refreshUser = 'gotoUser();';
                api.execScript({
                    name: 'home',
                    script: refreshUser,
                });

                // 成功后关闭登录页面
                api.closeToWin({
                    name: 'home'
                });

            } else {
                var param = api.pageParam.login;
                if (param == 'licai') {
                // 上页面刷新数据
                var refreshUser = 'gotoUser();';
                api.execScript({
                    name: 'home',
                    script: refreshUser,
                });
                api.closeFrame({
                    name: 'login'
                });

                // api.closeToWin({
                //     name: 'home'
                // });
            }else{
             sendMsg();
                // 登录成功后 跳转到user页面调用刷新数据方法
                api.setFrameGroupIndex({
                    name: 'nav',
                    index: 2,
                    reload: true,
                });
                api.closeFrame({
                    name: 'login'
                });
            }
        }
    }
};

    // toast提示框
    function showToast() {
        api.toast({
            msg: '登录成功',
            duration: 2000,
            location: 'middle'
        });

    }

    // 登录成功发送刷新请求
    function sendMsg() {
        api.sendEvent({
            name: 'login_success',
            extra: {
                key1: 'value1',
                key2: 'value2'
            }
        });
    }

    // open注册
    function register() {
        var delay = 0;
        if (api.systemType != 'ios') {
            // delay = 300;
        }
        api.openWin({
            name: 'register',
            url: './register.html',
            pageParam: {
                name: 'test'
            }
        });
    }

    // open忘记密码
    function forgetPwd() {
        api.openWin({
            name: 'forget-pwd',
            url: './forget-pwd.html',
            pageParam: {
                name: 'test'
            }
        });
    }

    // 修改密码之后,登录页面自动补充手机号
    function setMobile() {
        var mobile = $api.getStorage('mobile');
        if (mobile) {
            var mobileEle = $api.byId('mobile');
            mobileEle.value = mobile;
        }
    }

    function ios_successLogin(){
        // 登录成功后 跳转到user页面调用刷新数据方法
        api.setFrameGroupIndex({
            name: 'nav',
            index: 2,
            reload: true,
        });

        // 成功后关闭登录页面
        api.closeFrame({
            name: 'login'
        });
    }
</script>

</html>
